package com.plsjavaweb.core;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.plsjavaweb.dao.DAOFactory;
import com.plsjavaweb.dao.interfaces.ReportsDAO;
import com.plsjavaweb.logging.LoggingConstants;
import com.plsjavaweb.pojo.CSReportsPOJO;
import com.plsjavaweb.pojo.ProjectPOJO;
import com.plsjavaweb.pojo.SeatAllocationPOJO;

public class ReportsCore {
	Logger log = Logger.getLogger(DAOFactory.class);
	public CSReportsPOJO getTotalSeatReportsPerFloor(String floor) {
		log.info(LoggingConstants.entryMessage);
		
		StringBuilder validationMessage = new StringBuilder();
		boolean hasCredentials = false;

		CSReportsPOJO reports = new CSReportsPOJO();
		SeatAllocationPOJO empSeatAllocation = new SeatAllocationPOJO();
		SeatAllocationPOJO pmSeatAllocation = new SeatAllocationPOJO();

		ReportsDAO csDAO = DAOFactory.getReportDAO();
		empSeatAllocation = getEmployeeSeatAllocationPerFloor(floor);
		pmSeatAllocation = getPmSeatAllocationPerFloor(floor);

		reports.setEmployeeAllocated(empSeatAllocation.getSeatAllocated());
		reports.setEmployeeReserved(empSeatAllocation.getSeatReserved());
		reports.setEmployeeVacant(empSeatAllocation.getSeatVacant());
		reports.setPmAllocated(pmSeatAllocation.getSeatAllocated());
		reports.setPmReserved(pmSeatAllocation.getSeatReserved());
		reports.setPmVacant(pmSeatAllocation.getSeatVacant());
		reports.setFloorLevel(floor);
		
		log.info(LoggingConstants.returnedMessage);
		return reports;
	}

	public List<ProjectPOJO> getProjectSeatByManager(int managerId) {
		log.info(LoggingConstants.entryMessage);

		List<ProjectPOJO> projectSeatAllocation = new ArrayList<ProjectPOJO>();
		ReportsDAO csDAO = DAOFactory.getReportDAO();

		projectSeatAllocation = csDAO.getProjectReportsByProjectManager(managerId);

		for (ProjectPOJO projectPOJO : projectSeatAllocation) {
			SeatAllocationPOJO seatAllocationPOJO = getProjectSeatAllocation(projectPOJO.getProjectId());
			projectPOJO.setTotalSeats(seatAllocationPOJO.getSeatAllocated() + seatAllocationPOJO.getSeatReserved() + seatAllocationPOJO.getSeatVacant()); 
			projectPOJO.setManagerId(managerId);
			projectPOJO.setSeatAllocation(seatAllocationPOJO);
		}

		log.info(LoggingConstants.returnedMessage);
		return projectSeatAllocation;
	}
	
	public List<ProjectPOJO> getProjectSeat() {
		log.info(LoggingConstants.entryMessage);

		List<ProjectPOJO> projectSeatAllocation = null;
		ReportsDAO csDAO = DAOFactory.getReportDAO();

		projectSeatAllocation = csDAO.getProjectReports();

		for (ProjectPOJO projectPOJO : projectSeatAllocation) {
			SeatAllocationPOJO seatAllocationPOJO = getProjectSeatAllocation(projectPOJO.getProjectId());
			projectPOJO.setTotalSeats(seatAllocationPOJO.getSeatAllocated() + seatAllocationPOJO.getSeatReserved() + seatAllocationPOJO.getSeatVacant()); 
			//projectPOJO.setManagerId();
			projectPOJO.setSeatAllocation(seatAllocationPOJO);
		}

		log.info(LoggingConstants.returnedMessage);
		return projectSeatAllocation;
	}
	
	public CSReportsPOJO getProjectSeatReport(String floor) {
		log.info(LoggingConstants.entryMessage);

		StringBuilder validationMessage = new StringBuilder();
		boolean hasCredentials = false;

		CSReportsPOJO reports = new CSReportsPOJO();
		SeatAllocationPOJO empSeatAllocation = new SeatAllocationPOJO();
		SeatAllocationPOJO pmSeatAllocation = new SeatAllocationPOJO();

		ReportsDAO csDAO = DAOFactory.getReportDAO();
		empSeatAllocation = getEmployeeSeatAllocationPerFloor(floor);
		pmSeatAllocation = getPmSeatAllocationPerFloor(floor);

		reports.setEmployeeAllocated(empSeatAllocation.getSeatAllocated());
		reports.setEmployeeReserved(empSeatAllocation.getSeatReserved());
		reports.setEmployeeVacant(empSeatAllocation.getSeatVacant());
		reports.setPmAllocated(pmSeatAllocation.getSeatAllocated());
		reports.setPmReserved(pmSeatAllocation.getSeatReserved());
		reports.setPmVacant(pmSeatAllocation.getSeatVacant());

		log.info(LoggingConstants.returnedMessage);
		return reports;
	}
	
	private SeatAllocationPOJO getEmployeeSeatAllocationPerFloor(String floor){
		log.info(LoggingConstants.entryMessage);
		ReportsDAO csDAO = DAOFactory.getReportDAO();
		log.info(LoggingConstants.returnedMessage);
		return csDAO.getSeatAllocationForEmployeePerFloor(floor);
	}
	
	private SeatAllocationPOJO getPmSeatAllocationPerFloor(String floor){
		log.info(LoggingConstants.entryMessage);
		ReportsDAO csDAO = DAOFactory.getReportDAO();
		log.info(LoggingConstants.returnedMessage);
		return csDAO.getSeatAllocationForPMPerFloor(floor);
	}
	
	private SeatAllocationPOJO getProjectSeatAllocation(int projectId){
		log.info(LoggingConstants.entryMessage);
		ReportsDAO csDAO = DAOFactory.getReportDAO();
		log.info(LoggingConstants.returnedMessage);
		return csDAO.getSeatAllocationAllPerProject(projectId);
	}


}
